const app = getApp()

var util = require("../../../../common/util/util");

let innerAudioContext = wx.createInnerAudioContext();

Page({
    data: {
        xProduct : '',
        xProductnew:[],
        xPageReady: true,
        week:'',
        day:'',
        deadline:'',
        status:false,
        xUserInfo:{}
    },
    onLoad : function (options) {
        this.setData({
          week:Number(options.week),
          status:JSON.parse(options.status)
        })
        this.xInit(options.id)
    },
    xInit : function (id) {
        let that = this
        wx.cloud.callFunction({
          name: 'product',
          data: {
            // product_
            action: "product_detail",
            product_id: id
          }
        }).then(res => {
          let data = res.result
          that.setData({
              xProduct : data
          })
          that.xget()
        })
    },
    xget(){
      let that=this;
      let _obj={
        week:that.data.week,
        is_all :that.data.week ==0 ?true :false
      }
      wx.cloud.callFunction({
          name: 'digital_therapy',
          data: {
            action: "get_history_content",
            dataObj:_obj
          }
      }).then(res => {
        let data = res.result
        that.setData({
            xProductnew : data,
            xPageReady:false
        })
      })
    },
    xSet:function(){
        this.setData({
          status:false
        })
    },
    xToPlay: function ( e ) {
      let params = e.currentTarget.dataset
     
      wx.navigateTo({
          url: '/subtool/pages/rudio/detail/index',
          success: function(res) {
            // 通过eventChannel向被打开页面传送数据
            res.eventChannel.emit('xToAudioDetail', params)
          }
      })
    },
    xTovideo : function ( e ) {
      let params = e.currentTarget.dataset
      wx.navigateTo({
        url: '/subtool/pages/video/index/index',
          success: function(res) {
            // 通过eventChannel向被打开页面传送数据
            res.eventChannel.emit('xToAudioDetail', params)
          }
      })
    },
    xTocommunication:function(e){
      let that =this;
      let id= e.currentTarget.dataset.treatment_item_id
      let week =e.currentTarget.dataset.week
      wx.navigateTo({
        url: '/subtool/pages/communication/index/index?treatment_item_id='+ id +'&type=1'+'&week='+week
      })
    },
    // 用户综合测评
    xToUserDetail: function (e) {
      let param =e.currentTarget.dataset
      wx.navigateTo({
        url: '/subuser/pages/research/index?treatment_item_id='+  param.id+'&week=0',
      })
    },
    // 循环练习
    xToAction:function(e){
      let week =e.currentTarget.dataset.week
      let id = e.currentTarget.dataset.treatment_item_id
      let status = e.currentTarget.dataset.physiotherapy_today_completed_status
      wx.navigateTo({
          url: '/subtool/pages/action/index/index?week='+week+'&treatment_item_id='+id+'&type=2',
      })
    },
    // 放松练习
    xToRelaxaudio:function(e){
      let week =e.currentTarget.dataset.week
      let id = e.currentTarget.dataset.treatment_item_id
      let num =e.currentTarget.dataset.num
      wx.navigateTo({
          url: '/subtool/pages/relaxaudioall/index/index?week='+week+'&treatment_item_id='+id+'&type=2'+'&num='+num +'',
      })
    },
    // 治疗清单
    xTotreatmentlist:function(e){
      let _id =e.currentTarget.dataset.treatment_item_id
      wx.navigateTo({
        url: '/subtool/pages/treatmentlist/index/index?treatment_item_id='+ _id+'&type=2'
      })
    },
    // 压力源
    xToPressuresource:function(e){
      let that =this;
      let id= e.currentTarget.dataset.treatment_item_id
      let week =e.currentTarget.dataset.week
      wx.navigateTo({
        url: '/subtool/pages/pressuresource/index/index?treatment_item_id='+ id +'&type=2'+'&week='+week
      })
    },
    todayremb: function(e){
      let name =e.currentTarget.dataset.name;
      let id =e.currentTarget.dataset.id;
      let num =e.currentTarget.dataset.num;
      let week= e.currentTarget.dataset.week
      wx.navigateTo({
        url: '/subtool/pages/dayremb/detail/index?id=' + id + '&num=' + num + '&week='+week +'&title=' + name+'&day=0'+'&treatment_item_id='+id+'&type=2',
      })
    },
    // 制定目标
    xToTarget:function(e){
      let week=e.currentTarget.dataset.week
      let day =e.currentTarget.dataset.day
      let treatment_item_id= e.currentTarget.dataset.treatment_item_id
      let index  =e.currentTarget.dataset.index
      let video = e.currentTarget.dataset.video
      wx.navigateTo({
          url: '/subsale/pages/product/targetMilepost/index?week='+week+'&day='+day+'&treatment_item_id='+ treatment_item_id+'&video='+ video,
      })
    },
    // 患教内容
    xToTech: function (e) {
      let data = e.currentTarget.dataset
      let params = {
          index: data.index ,
          content: JSON.stringify(data.content) ,
          week: data.week ,
          ava : data.ava ,
          cid : data.cid ,
          day: data.day,
          education_content_id: data.education_content_id,
          id:data.id,
          type:2
      }
      wx.navigateTo({
        url: '/subsale/pages/product/tech/index?education_content_id='+data.education_content_id+'&type=2&week='+data.week+'&day='+data.day
    })
  },
   //前往疼痛基线日记
   xToPaindiarybaseline: function ( e ){
    let week=e.currentTarget.dataset.week
    let day =e.currentTarget.dataset.day
    let treatment_item_id= e.currentTarget.dataset.treatment_item_id
    wx.navigateTo({
        url: '/subsale/pages/product/paindiaryline/index/index?week='+week+'&day='+day+'&treatment_item_id='+ treatment_item_id +'&index='+index,
    })
  }, 
  //前往疼痛日记
  xToPaindiary: function ( e ){
    let week=e.currentTarget.dataset.week
    let day =e.currentTarget.dataset.day
    let treatment_item_id= e.currentTarget.dataset.treatment_item_id
    let index  = e.currentTarget.dataset.index
    wx.navigateTo({
      url: '/subsale/pages/product/paindiary/index?week='+week+'&day='+day+'&treatment_item_id='+ treatment_item_id+'&index='+index+'&type=2',
    })
  }, 
    // 返回上一页
    xBack: function () {
        wx.navigateBack({
            delta: 1 ,
            fail : () => {
                wx.switchTab({
                    url: '/pages/navigation/index/index',
                })
            }
        })
    },

    xClean : function (e) {
        let that = this
        let index = e.currentTarget.dataset.index
        var keys = 'xProductnew['+index+ '].show'
        that.setData({
          [keys]:!that.data.xProductnew[index].show
        })
    },
    xToall : function ( ) {
        let that = this;
        for(let index in that.data.xProductnew){
         if( that.data.xProductnew[index].status==1){
          var keys = 'xProductnew['+index+ '].show'
          that.setData({
            [keys]:!that.data.xProductnew[index].show
          })
         }
        }
    },
    player: function (e) {
        let that = this
        let params = e.currentTarget.dataset
        // let data = that.data.data
        // data.recordList[params.nindex].summary[params.rindex].content.record = false
        that.setData({
            playStatus: false ,
            data: data
        })
        var musicUrl = params.vocd
        console.log('musicUrl', musicUrl)
        innerAudioContext.src = musicUrl;
        innerAudioContext.autoplay = true
        innerAudioContext.play();
        innerAudioContext.onEnded(() => {
            that.setStopState(params)
        })
    },

    setStopState : function (params) {
        let that = this
        let data = that.data.data
        // data.recordList[params.nindex].summary[params.rindex].content.record = true
        that.setData({
            playStatus: false ,
            data: data
        })
    },
    // 获取患教内容
    xReader: function (week) {
        let that = this
        let num = that.data.product_number
        wx.cloud.callFunction({
            name: 'digital_therapy',
            data: {
                action: "get_cognitive_education_content",
                dataObj: {
                    product_number : num ,
                    week: week,
                }
            }
        }).then( res => {
            console.log(res)
            that.setData({
                read : res.result
            })
        })
    }
})